package com.changan.config;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class SqlConfig {
    private final static String getLatestMetadataTables = "SELECT * FROM `table_info` where database_name= ? ";
    private final static String getLatestMetadataTable = "SELECT * FROM `table_info` WHERE database_name = ? AND table_name = ? ";
    private final static String getLatestMetadataColumns = "SELECT * FROM column_info WHERE database_name= ? AND table_name = ?  ORDER BY is_primary_key DESC,col_position ASC";

    private final static String getJobInfoDetails = "" +
            " SELECT \n" +
            " job_name,\n" +
            " source_table_name, source_pk,target_name, target_table_name,target_pk, kafka, kafka_topic, c.source_address as kafka_address,kafka_pk,b.source_name, b.source_address, b.user_name, b.`password`, b.`host`, b.`port`,b.database_name,\n" +
            " d.source_address as target_address,\n" +
            " d.user_name as target_user_name,\n" +
            " d.`password` as target_password\n" +
            " FROM input_data_sources a\n" +
            " LEFT JOIN data_sources b \n" +
            " ON a.source_name = b.source_name\n" +
            " LEFT JOIN data_sources c \n" +
            " ON a.kafka = c.source_name \n" +
            " LEFT JOIN data_sources d \n" +
            " ON a.target_name=d.source_name" +
            " WHERE a.id=?";

    private final static String getKafkaToStarrocksJobInfo = "";


    public static PreparedStatement getMetadataTables(Connection conn, String databaseName) throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement(getLatestMetadataTables);
        preparedStatement.setString(1, databaseName);
        return preparedStatement;
    }

    public static PreparedStatement getMetadataTable(Connection conn, String databaseName, String tableName) throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement(getLatestMetadataTable);
        preparedStatement.setString(1, databaseName);
        preparedStatement.setString(2, tableName);
        return preparedStatement;
    }

    public static PreparedStatement getMetadataColumns(Connection conn, String databaseName, String tableName) throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement(getLatestMetadataColumns);
        preparedStatement.setString(1, databaseName);
        preparedStatement.setString(2, tableName);
        return preparedStatement;
    }

    public static PreparedStatement getStartJobInfoDetails(Connection conn, Integer id) throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement(getJobInfoDetails);
        preparedStatement.setInt(1, id);
        return preparedStatement;
    }

}
